<?php
//strlen\mb_strlen字符串长度
$a='123计算机';
echo strlen($a),"\n";//字节的个数 // 12
echo mb_strlen($a),"\n";//字符的个数 //6
$b=iconv('utf-8','GBK',$a);
echo strlen($b),"\n";// 9 字符串显示时的宽度，1个汉字是两个英文字符的宽度
//strcmp用于比较两个字符串的大小
echo strcmp("Hello","Hi"),"\n"; //-1
echo strcasecmp("hi","Hi"),"\n"; //0
echo strcasecmp("hi~","Hi"),"\n"; //1
//trim删除字符串首尾的空白
$s="   \t \t How are you?  \t  ";
echo "[$s]\n"; //[   	 	 How are you?  	  ]
echo "[", trim($s),"]\n"; //[How are you?]
echo "[", ltrim($s),"]\n"; //[How are you?  	  ]
echo "[", rtrim($s),"]\n"; //[   	 	 How are you?]
$s=",:.How are you?____";
echo "[", trim($s,"_,:."),"]\n"; //[How are you?]
//strpos查找子串在父字符中的位置，i忽略大小写，r相反的顺序
echo strpos('计算机程序设计','程序');   // 显示 9
echo stripos("I'm Happy.",'h');   // 显示 4
echo strripos("How old are you?",'o');   // 显示 13
var_dump(strpos('计算机程序设计','小程序'));   //bool(false) 没有找到
//bin2hex将字符串的每个字节依次转换为两个16进制字符
$a='123计算机';
echo bin2hex($a),"\n"; //313233e8aea1e7ae97e69cba
//chunk_split将字符串按指定的长度用指定的字符分隔开来
echo chunk_split(bin2hex($a),2,'_'),"\n"; //31_32_33_e8_ae_a1_e7_ae_97_e6_9c_ba_
//str_replace字符串替换
echo str_replace('o','ooo',"How are you?\n");//Hooow are yooou?
echo str_replace(['o', 'r'],'oo',"How are you?\n");//Hoow aooe yoou?
echo str_replace(['o', 'r'],['O','R'],"How are you?\n");//HOw aRe yOu?
echo str_replace(['o','r',' '],'',"How are you?\n");//Hwaeyu?
print_r(str_replace(['o', 'r'],['O','R'],["How are you?",'So good']));
/*
Array
(
    [0] => HOw aRe yOu?
    [1] => SO gOOd
) */
//str_repeat重复字符串若干次, 0次会返回空字符串""
echo str_repeat('Ok',3),"\n"; // OkOkOk
echo str_repeat('-',60),"\n";
//join 拼接数组元素为一个字符串
echo join(['Hi','Zhangsan']),"\n"; //HiZhangsan
echo join(', ', [123,456,789]),"\n"; //123, 456, 789


//将数组中的每项数据及其对应的 16 进制串，两端对齐显示在指定的宽度内，若宽度不足，则两项数据直接显示。
$a = ['张三丰', '里  斯', '阿 kit', 'papi 酱', 'Jackson'];
outFormat(30, $a); //利用函数处理数组数据, 输出内容在35个英文字符宽度内

//完善函数功能，将数组中的每项数据及其16进制串，两端对齐显示在指定的宽度内
//数组中的每一项数据中包含的空格要删除掉
//16进制串中每个字节占两个16进制位，英文字符采用大写，各字节间以_分隔
//本文件存储采用utf-8编码
function outFormat(int $width, ?array $a):void {
    if(is_null($a))return;
    foreach ($a as $item){
        $name=str_replace(' ','', $item); //把姓名中的空白删除掉
        $right=bin2hex($name); //把字符串的每一个字节转换为2位的16进制 e5bca0e4b889e4b8b0
        $right=strtoupper($right);//把字符串转换为大写 E5BCA0E4B889E4B8B0
        $right=chunk_split($right,2,'_');//把字符串2位一组用_分隔开来 E5_BC_A0_E4_B8_89_E4_B8_B0_
        //$right=rtrim($right, '_'); //删除右侧的所有的_  E5_BC_A0_E4_B8_89_E4_B8_B0
        //$right=substr($right,0, strlen($right)-1); //E5_BC_A0_E4_B8_89_E4_B8_B0
        $right=substr($right,0, -1); //E5_BC_A0_E4_B8_89_E4_B8_B0
        $rightLength=strlen($right);
        $nameLength=strlen( iconv('utf-8','GBK',$name) );
        $spaceLength=$width-$nameLength-$rightLength;//中间空白部分的长度
        if($spaceLength<0)$spaceLength=0; //宽度不足，直接显示
        echo $name, str_repeat(' ',$spaceLength),$right,"\n";
    }
}
/* 输出内容如下：
张三丰   E5_BC_A0_E4_B8_89_E4_B8_B0
里斯              E9_87_8C_E6_96_AF
阿kit             E9_98_BF_6B_69_74
papi酱         70_61_70_69_E9_85_B1
Jackson        4A_61_63_6B_73_6F_6E
*/